#region Using

using System;
using System.Collections.Generic;
using System.Linq;

using Gizmox.WebGUI.Forms;
using FlexCel.Render;
using FlexCel.Core;
using System.IO;
using TVC.Report;
using Gizmox.WebGUI.Common.Gateways;

#endregion

namespace TVC.Modules.Rep
{
    public partial class frmViewReport : Form
    {
        public string filename = "";
        public MainForm MainForm { get; set; }
        public frmViewReport(string filename, MainForm form)
        {
            Text = filename;
            InitializeComponent();
            MainForm = form;
        }
        public ExcelFile Xls { get; set; }

        public string LoadGadget(ref String pathfile, string pathHTML, string htmlHandle)
        {
            pathfile = ExportHTML(String.Format("{0}Documents\\Report\\{1}\\", MainForm.ServerPath, MainForm.PODInfo.USER_ID), filename);
            string content = "";
            if (!File.Exists(pathfile))
                return "";
            using (StreamReader sr = new StreamReader(pathfile))
            {
                content = sr.ReadToEnd();
            }
            content = content.Replace("{0}", htmlHandle);
            try
            {
                using (FileStream fs = File.Create(pathfile))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.Write(content);
                    }
                }
                String[] s = pathfile.Split('\\');
                return String.Format("/{0}/{1}", pathHTML, s[s.Length - 1]);
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
        private string ExportHTML(string path, string code)
        {
            String filename = String.Format("{0}{1}.htm", path, code);
            //TextWriter file = new StringWriter(); ;
            try
            {
                using (FlexCelHtmlExport html = new FlexCelHtmlExport() { Workbook = Xls })
                {
                    THtmlExtraInfo inf = new THtmlExtraInfo();
                    html.ExtraInfo.HeadStart = new string[] { "<script src='Resources.Gizmox.WebGUI.Forms.Skins.CommonSkin.Interfaces.js.wgx' type='text/javascript'></script>", "<script language='javascript' type='text/javascript'>", "function tavico_link(cmd,args) {", "var objEvent = VWG.Events.CreateEvent('{0}', 'TaViCo_Link');", "VWG.Events.SetEventAttribute(objEvent, cmd, args);", "VWG.Events.RaiseEvents();}", "</script>", "<script language='JavaScript' src='/FusionCharts/FusionCharts.js'></script>" };
                    html.AllowOverwritingFiles = true;
                    html.SavedImagesFormat = THtmlImageFormat.Png;
                    html.Export(filename, "images", String.Format("css\\{0}.css", code));
                }
                return filename;
            }
            catch
            {
                return "";
            }

        }
        private void ThrowMsg(string sErr)
        {
            BaseForm x = Parent.Parent as BaseForm;
            if (x != null)
            {
                x.SetMsg(sErr);
            }
        }

        private void frmViewReport_Load(object sender, EventArgs e)
        {
            //myHtmlbox1.Url = LoadGadget(ref filename, "Documents/Report/" + MainForm.PODInfo.USER_ID, myHtmlbox1.Handle.ToString());

            Report.ReportGenerator.ExportHTML2String("Documents/Report/" + MainForm.PODInfo.USER_ID, Xls);
            myHtmlbox1.Html = Report.ReportGenerator.ExportHTML2String("Documents/Report/" + MainForm.PODInfo.USER_ID, Xls);// LoadGadget(ref filename, "Documents/Report/" + MainForm.PODInfo.USER_ID, myHtmlbox1.Handle.ToString());
            myHtmlbox1.Update();
        }


        private void btExcel_Click(object sender, EventArgs e)
        {
            ExcelFile file = Xls;
            MemoryStream ms = new MemoryStream();

            file.Save(ms);
            DownloadGateway myDownload = new DownloadGateway(ms, filename + ".xls");

            myDownload.ContentType = "application/vnd.ms-excel";
            //myDownload.DownloadAsAttachment = true;

            //Start Download
            myDownload.StartDownload(this);
            //FileDownloadGateway myDownload = new FileDownloadGateway() { Filename = filename + ".xls" };

            //myDownload.SetContentType(FileDownloadGateway.DownloadContentType.MicrosoftExcel);
            //myDownload.StartBytesDownload(this, ms.ToArray());
        }

        private void btPdf_Click(object sender, EventArgs e)
        {
            ExcelFile file = Xls;
            MemoryStream ms = new MemoryStream();

            try
            {
                if (file != null)
                {
                    FlexCel.Render.FlexCelPdfExport pdf = new FlexCel.Render.FlexCelPdfExport(file);
                    pdf.Export(ms);
                }
            }
            catch { }

            DownloadGateway myDownload = new DownloadGateway(ms, filename + ".pdf");

            myDownload.ContentType = "application/x-pdf";
            myDownload.StartDownload(this);
        }
    }
}